#include "KmyMacroDict.h"

KmyMacroDict::KmyMacroDict()
{
}

KmyMacroDict::~KmyMacroDict()
{

}

void KmyMacroDict::initialize(KmyScript *macro_script){

}

void KmyMacroDict::finalize(){
    if (!macro_map.empty()){
        map<string, KmyScriptAnchor*>::iterator iter = macro_map.begin();
        for (; iter != macro_map.end(); iter++){
            delete iter->second;
        }
        macro_map.clear();
    }
}

KmyScriptAnchor* KmyMacroDict::putScriptAddr(KmyScriptAnchor *sa){
    KmyScriptAnchor *rt = getScriptAddr(sa->script_name);
    if (rt!=NULL){
        removeScriptAddr(sa->script_name);
    }
    macro_map.insert(pair<string, KmyScriptAnchor*>(sa->script_name, sa));
    return rt;
}

KmyScriptAnchor* KmyMacroDict::getScriptAddr(const string &macro_name){
    map<string, KmyScriptAnchor*>::iterator iter = macro_map.find(macro_name);
    if (iter!=macro_map.end()){
        return iter->second;
    }
    return NULL;
}

KmyScriptAnchor* KmyMacroDict::removeScriptAddr(const string &macro_name){
    KmyScriptAnchor *rt = getScriptAddr(macro_name);
    if (rt!=NULL){
        macro_map.erase(macro_name);
    }
    return rt;
}
